package com.ajie.controller;

import com.ajie.service.SysUserService;
import com.ajie.utils.RedisUtil;
import com.ajie.utils.Result;
import com.ajie.utils.SecurityUtil;
import com.ajie.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;


/**
 * 登录以及退出
 * 获取当前登录用户的基本信息
 * 相关接口
 *
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户使用接口")
@Slf4j
public class LoginController {
    
    @Autowired
    private SysUserService userService;
    
    @Autowired
    private RedisUtil redisUtils;
    
    
    /**
     *登录接口
     * 登录参数：账号和密码
     * @return返回token ，用token去获取资源
     */
    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo){
        log.info("该文件名是：LoginController,该方法是：login");
        return userService.login(loginVo);
    }
    
    @ApiOperation(value = "短信登录")
    @PostMapping("/sms/login")
    public Result smsLogin(@RequestBody LoginVo loginVo){
        log.info("该文件名是：LoginController,该方法是：smsLogin");
        return userService.login(loginVo);
    }

    @ApiOperation(value = "获取用户基本信息")
    @GetMapping("getInfo")
    public Result getUserInfo(){
        log.info("该文件名是：LoginController,该方法是：getUserInfo");
        return Result.success("获取用户信息成功", SecurityUtil.getUser());
    }

    @ApiOperation(value = "用户退出登录")
    @GetMapping("/logout")
    public Result logout(){
        log.info("该文件名是：LoginController,该方法是：logout");
        redisUtils.delKey("userInfo_" + SecurityUtil.getUsername());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication !=null){
            SecurityContextHolder.getContext().setAuthentication(null);
        }
        return Result.success("退出成功！");
    }
    

    
  

}





















